package edu.infomatik.tundra.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.EnumSelectModel;

import edu.infomatik.tundra.data.DAOtundra;
import edu.infomatik.tundra.data.IBean;
import edu.infomatik.tundra.logic.SprintBean;
import edu.infomatik.tundra.logic.SprintMetrics;
import edu.infomatik.tundra.logic.UStoryTypes;
import edu.infomatik.tundra.logic.WorkBean;
import edu.infomatik.tundra.logic.WorkKinds;
import edu.infomatik.tundra.logic.security.SystemAccessBean;
import edu.infomatik.tundra.model.ProjectIdEncoder;
import edu.infomatik.tundra.model.ProjectIdSelectModel;
import edu.infomatik.tundra.data.DataUtils;

@SuppressWarnings("unused")
public class CreateSprint {
	@SessionState
	@Property
	private SprintBean sprint;
	@SessionState
	private SystemAccessBean user;
	private boolean userExists;
	@Inject
	private Messages messages;
	@SessionState
	private Boolean newWork;
	private WorkBean work;
	private SprintMetrics selectedMetrics;
	@Property
	private String projectIdLabel;
	@Property
	private String metricsLabel;

	public SprintMetrics getSelectedMetrics() {
		return selectedMetrics;
	}
	public void setSelectedMetrics(SprintMetrics selectedMetrics) {
		this.selectedMetrics = selectedMetrics;
		sprint.setMetrics(selectedMetrics);
	}
	public SelectModel getsKind() {
		return new EnumSelectModel(WorkKinds.class, messages);
	}
	public SelectModel getsMetrics() {
		return new EnumSelectModel(SprintMetrics.class, messages);
	}
	public String getSelectKind(){
		return messages.get("WorkKinds."+WorkKinds.get(sprint.getKind()).getName());
	}
	public short[] getKinds(){
		short[] result= new short[WorkKinds.values().length];
		int i=0;
		for (WorkKinds wk :WorkKinds.values()){
			result[i]=(short)wk.getValue();
		}
		return result;
	}
	String onActivate(){
		projectIdLabel="<br/>.";
		if (!userExists) return "Index";
		if (null == sprint || sprint.getId()==0 || newWork.booleanValue()){
			sprint = new SprintBean();
			sprint.setId(DAOtundra.getInstance().getMaxId()+1);
			sprint.setDateCreated(DataUtils.getNow());
		}else{
			metricsLabel = "(<i>"+messages.get("SprintMetrics."+SprintMetrics.get(sprint.getMetrics()).getName())+"</i>)";
			WorkBean b = new WorkBean();
			// adding text to projectId-Label 
			if (sprint.getProjectId()>0){
				b.setId(sprint.getProjectId());
				projectIdLabel="<br/>(<i>"+((WorkBean)DAOtundra.getInstance().getBeanById(b)).getName()+"</i>)";
			}
		}
		newWork=new Boolean(false);
		return null;
	}
	public long getSprintId(){
		return sprint.getId();
	}
	
	void onSubmitFromSprint(){
		DAOtundra.getInstance().update(sprint);
	}
	
	// Begin: methods for selectProject
	public List<WorkBean> getAllProjects(){
		List<WorkBean> wbList= new ArrayList<WorkBean>();
		for (IBean wb: DAOtundra.getInstance().getAllBeans(new WorkBean())){
			if (((WorkBean)wb).getKind()==WorkKinds.PROJECT_VALUE){
				wbList.add((WorkBean)wb);
			}
		}
		return wbList;
	}
	
	public SelectModel getProjectIdModel(){
		return new ProjectIdSelectModel(getAllProjects());
	}
	public ValueEncoder<WorkBean> getProjectIdEncoder(){
		return new ProjectIdEncoder();
	}
	
	public WorkBean getProjectIdSelection(){
		return work;
	}
	public void setProjectIdSelection(WorkBean selectedProject){
		work = selectedProject;
		sprint.setProjectId(selectedProject.getId());
	}
	// END: methods for selectProject
}
